package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.ReceiveCode;

public class ReceiveCodeDAO {

	/** a database of this Receive_code */
    private Database db;
    
    /**
     * Instantiate a new object of this class
     * @param database a database
     */
    public ReceiveCodeDAO(Database db) {
        this.db = db;
    }
    
    /**
     * a method for searching all receive_code in database
     * @return all receive_code
     */
    public List<Map<String, Object>> findAll() {
        String vaSql = "SELECT * FROM receive_code ORDER BY receive_code ASC";
        List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
    /**
     * a method for searching the receive_code by site in database
     * @return receive_code where are in the site
     */
    public List<Map<String, Object>> findReceive_CodeBySite(int pnSiteId) {
        String vaSql = "SELECT receive_code, description , HAVE_VAT FROM receive_code WHERE site_id = ? ORDER BY receive_code ASC";
        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

    public String findFileBySiteAndReceiveCode(int pnSiteId,String paCode , String paField) {
        String vaSql = "SELECT " + paField + " FROM receive_code WHERE site_id = ? and receive_code = ?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paCode);
        if (result != null) {
            return (String) result.get(paField).toString();
        } else {
            return null;
        }
    }
    
    /**
     * a method for adding a receive_code into database
     * @param poReseiveCode  to add 
     */
    public void addReceiveCode(ReceiveCode poReceiveCode) {
    	 String vaSql = "INSERT INTO receive_code (SITE_ID,RECEIVE_CODE,DESCRIPTION,HAVE_VAT,VALUE_ADD,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,"
    	 								+ "UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)"
    			 						+"VALUES(?,?,?,?,?,?,?,?,?,?,?)";
    			try {
    					db.insert(vaSql, poReceiveCode.getVnSiteId(),
    							poReceiveCode.getVnReceiveCode(),
    							poReceiveCode.getVaDescription(),
    							poReceiveCode.getVaHaveVat(),
    							poReceiveCode.getVaValueAdd(),
    							poReceiveCode.getVnInsertUserId(),
    							poReceiveCode.getVaInsertDate(),
    							poReceiveCode.getVaInsertTime(),
    							poReceiveCode.getVnUpdateUserId(),
    							poReceiveCode.getVaUpdateDate(),
    							poReceiveCode.getVaUpdateTime());
    				
    			}catch (Exception e) {
    	        	throw new RuntimeException(e);
    	        }
    	}
    
  //Update Receive_code
    public void upDateReceiveCode(int pnSiteId,String pnReceiveCode,ReceiveCode poNewReceiveCode) {
    	 //String vaSql = "UPDATE ZONE SET ZONE_NAME = ? ,UPDATE_USER_ID=?,UPDATE_DATE=?,UPDATE_TIME=?"
    	 	// 	+ "WHERE SITE_ID=? and ZONE_ID=?";
    	 	 
    	String vaSql  = " UPDATE RECEIVE_CODE SET " +
    				  " RECEIVE_CODE = ? , " +
    				  " DESCRIPTION  = ? , " +
    				  " HAVE_VAT  = ? , " +
    				  " UPDATE_USER_ID = ? , "+
    				  " UPDATE_DATE = ? , " +
    				  " UPDATE_TIME = ? " +
    				  " WHERE " + 
    				  " RECEIVE_CODE = ? " +
    				  " AND SITE_ID = ? ";
    	
    	 try {
				db.update(vaSql,poNewReceiveCode.getVnReceiveCode(),
						poNewReceiveCode.getVaDescription(),
						poNewReceiveCode.getVaHaveVat(),
						poNewReceiveCode.getVnUpdateUserId(),
						poNewReceiveCode.getVaUpdateDate(),
						poNewReceiveCode.getVaUpdateTime(),
						pnReceiveCode,
						pnSiteId
						);
							
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
    }
    
    
    public boolean deleteReseiveCodeByReceiveCode(int pnSiteId,String paReceiveCode) {
        String vaSql = "DELETE FROM RECEIVE_CODE WHERE SITE_ID = ? and RECEIVE_CODE = ?";
        try {
            db.update(vaSql, pnSiteId, paReceiveCode);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    
    public String findHaveVatBySiteAndReceiveCode(int pnSiteId,String paCode) {
        String vaSql = "SELECT  have_vat FROM receive_code WHERE site_id = ? and receive_code = ? AND have_vat <> 0";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paCode);
        if (result != null) {
            return (String) result.get("HAVE_VAT");
        } else {
            return null;
        }
    }
    public String findReceiveCodeBySiteAndReceiveCode(int pnSiteId,String paCode) {
        String vaSql = "SELECT  receive_code FROM receive_code WHERE site_id = ? and receive_code = ?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paCode);
        if (result != null) {
            return (String) result.get("RECEIVE_CODE");
        } else {
            return null;
        }
    }
    
    public String getDescriptionByReceiveCode (int pnSiteId, String paReceiveCode) {
    	String vaSql = "SELECT description FROM receive_code WHERE site_id = ? AND receive_code = ?";
    	Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paReceiveCode);
        if (result != null) {
            return result.get("DESCRIPTION").toString();
        } else {
            return "";
        }
    }
    
    //get receive_code check dupicate for update
    public String findsReceiveCodeBySiteAndReceiveCode(int pnSiteId,String paOldCode,String paNewCode) {
        String vaSql = "SELECT  receive_code FROM receive_code WHERE site_id = ? and receive_code != ? and receive_code=?";
        Map<String, Object> result = db.querySingle(vaSql, pnSiteId,paOldCode,paNewCode);
        if (result != null) {
            return (String) result.get("RECEIVE_CODE");
        } else {
            return null;
        }
    }
    public List<Map<String, Object>> findReceiveCodeByCondition(int pnSiteId,String paCondition) {
    	
        String vaSql = "SELECT * FROM receive_code WHERE site_id = "+pnSiteId+" AND "+paCondition;
        List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }
	
}
